//
//  ScopedPointer.h
//  CPPLearning
//
//  Created by Xiao  Feng on 23/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#ifndef CLASSES_GAME_UTIL_SCOPEDPOINTER_H_
#define CLASSES_GAME_UTIL_SCOPEDPOINTER_H_

template<typename T>
class ScopedPointer
{
public:
    typedef T DataType; 
    
    explicit ScopedPointer(DataType* pData);
    ~ScopedPointer();
    
    DataType& operator*();
    DataType* operator->();
	DataType* get();
    
    void reset(DataType* pData = NULL);
    void swap(ScopedPointer<DataType>& other);
    
private:
    ScopedPointer(const ScopedPointer& other);
    ScopedPointer& operator=(const ScopedPointer& other);
    
private:
    DataType* m_pData;
};

template<typename T>
ScopedPointer<T>::ScopedPointer(ScopedPointer::DataType* pData)
{
    m_pData = pData;
}

template<typename T>
ScopedPointer<T>::~ScopedPointer()
{
    delete m_pData;
}

template<typename T>
typename ScopedPointer<T>::DataType& ScopedPointer<T>::operator*()
{
    return *m_pData;
}

template<typename T>
typename ScopedPointer<T>::DataType* ScopedPointer<T>::operator->()
{
    return m_pData;
}

template<typename T>
typename ScopedPointer<T>::DataType* ScopedPointer<T>::get()
{
    return m_pData;
}

template<typename T>
void ScopedPointer<T>::reset(DataType* pData)
{
    delete m_pData;
    m_pData = pData;
}

template<typename T>
void ScopedPointer<T>::swap(ScopedPointer<DataType>& other)
{
    DataType* temp = m_pData;
    m_pData = other.m_pData;
    other.m_pData = temp;
}

template<typename T>
void swap(ScopedPointer<T>& left, ScopedPointer<T>& right)
{
    left.swap(right);
}

#endif //CLASSES_GAME_UTIL_SCOPEDPOINTER_H_
